<?php

class Sistema_UsuariosvendedorController extends App_Controller_Action
{
    
    private $_table     = 'USUARIOS';
    private $_pk        = 'USU_ID';
    private $_order_col = 'USU_DATCAD';
    private $_order     = 'ASC';
    private $_sta_col   = 'USU_STA';
    private $obj        = '';
    private $form       = '';
    //public  $db = '';
    
    private $_PER_ID = 4; // Vendedor
    private $_USU_TPO = 11; // escolar
    
    public function init()
    {
        //$this->db = Zend_Db_Table_Abstract::getDefaultAdapter();
        //$this->obj = new Sistema_Model_DbTable_Usuarios();
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Usuariosvendedor();   
    }
    
    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        
        $where = ' u.USU_TPO = '.$this->_USU_TPO;
        
        $USU_STA = $this->_getParam('USU_STA', 1);
        $where .= ' and u.USU_STA = '.$USU_STA;
        
        $query = 'SELECT SQL_CACHE u.* FROM '.$this->_table.' u WHERE '.$where.' ORDER BY u.'.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
        
        $this->view->db = $this->db;
    }
    
    public function editAction() {

        $this->form->submit->setLabel('Gravar');
        $this->view->form = $this->form;
          
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_usuarioesc.js?1=2');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskMoney.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mask.js');
        
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if ($this->form->isValid($formData)) {
                    
                    $id = (int) $formData[$this->_pk];
                    
                    $data = array(
                        'USU_ID'        => $formData['USU_ID'],
                        'USU_LOGIN'     => $formData['USU_LOGIN'],		
                        'USU_NOME'      => $formData['USU_NOME'],
                        'USU_EMAIL'     => $formData['USU_EMAIL'],
                        'PER_ID'        => $this->_PER_ID,
                        'USU_DATCAD'    => date('Y-m-d'),
                        'USU_TPO'       => $this->_USU_TPO,
                        'USU_CADSTA'    => 1,
                        'USU_STA'       => $formData['USU_STA'], 
                        'USU_CPF'       => $formData['USU_CPF'],  
                        'USU_END'       => $formData['USU_END'],    
                        'USU_TEL'       => $formData['USU_TEL'],
                    );

                    if(strlen($formData['USU_SENHA']) > 1){
                        $data['USU_SENHA'] = sha1($formData['USU_SENHA']);
                    }
                    
                    if ($id == 0){
                        $this->db->insert($this->_table, $data);
                        $id =  $this->db->lastInsertId();
                    }
                    else
                        $this->db->update($this->_table, $data,$this->_pk.' = ' . $id);

                    $this->_helper->redirector('index');
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.Errordb::descricaoError($ex->getCode()).'");</script>';
                $this->form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE USU_ID = '. $id;
                $result = $this->db->fetchRow($query);
                $this->form->populate($result);
            }
        }
    }

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $data = array('USU_STA' => 0);
            $ok = (bool) $this->db->update($this->_table,$data,$this->_pk . ' = ' . $id);
        }
        
        echo json_encode(array('ok' => $ok));
    }

}